package pathx

import (
	"os"
	"path/filepath"
)

func FindPathHasChild(name string, isDIR bool, dirs ...string) string {
	if len(dirs) == 0 {
		dirs = []string{"."}
	}

	for _, dir := range dirs {
		if !filepath.IsAbs(dir) {
			if dir == "" {
				dir = "."
			}
			if dir, _ = filepath.Abs(dir); dir == "" {
				return ""
			}
		}

		for i := 0; i < 30; i++ {
			if st, _ := os.Stat(filepath.Join(dir, name)); st != nil {
				if isDIR && st.IsDir() {
					return dir
				}
				if !isDIR && !st.IsDir() {
					return dir
				}
			}
			n := filepath.Dir(dir)
			if n == "" || n == dir {
				break
			}
			dir = n
		}
	}

	return ""
}

